package com.itheima.service.impl;

import com.itheima.dao.AdminDao;
import com.itheima.domain.Admin;
import com.itheima.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("adminService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) // 只读型配置
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    @Override
    public Admin findById(Integer adminId) {
        return adminDao.findById(adminId);
    }

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false) // 读写型
    @Override
    public void transfer(String sourceName, String targetName, Double money) {
        Admin sourceAdmin = adminDao.findByName(sourceName);
        Admin targetAdmin = adminDao.findByName(targetName);
        sourceAdmin.setMoney(sourceAdmin.getMoney() - money);
        targetAdmin.setMoney(targetAdmin.getMoney() + money);
        int sourceRow = adminDao.updateAdmin(sourceAdmin);
        int targetRow = adminDao.updateAdmin(targetAdmin);
        System.out.println("source: " + sourceRow + " | target: " + targetRow);
    }
}
